package com.wytest.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wytest.entity.Event;
import com.wytest.service.EventService;

@Controller
public class EventController {

	@Resource
	private EventService eventService;
	
	@RequestMapping(value="getall")
	public ModelAndView getAll(){
		List<Event> list=eventService.getAll();
		ModelAndView mav = new ModelAndView();
		mav.addObject("list", list);
		mav.setViewName("event");	
		return mav;
	}
	
	@RequestMapping(value="getadd")
	public ModelAndView getadd(){
		List<Event> list=eventService.getAll();
		ModelAndView mav = new ModelAndView();
		mav.addObject("list", list);
		mav.setViewName("addwork");	
		return mav;
	}
	
	@RequestMapping(value="add")
	public String add(Event event,int term, HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		if(term==0){int i=eventService.add(event);
		if(i>0){
			return "redirect:getall.do";
		}else{
			request.setAttribute("message", "保存失败");
			request.getRequestDispatcher("jsp/error.jsp").forward(request, response);
		}
		}else{
			eventService.update(event);
			return "redirect:getall.do";	
		}
		return null;
	}
	/*
	//修改事件
		@RequestMapping("update")
		public String update(Event event){
			eventService.update(event);
			return "redirect:getall.do";	
		}
		*/
		//根据ID查询
		@RequestMapping("showbyid")
		public ModelAndView showById(int eid,int term){
		Event event=eventService.showbyid(eid);
		ModelAndView mav=new ModelAndView();
		mav.setViewName("add");
		mav.addObject("term",term);
		mav.addObject("event",event);
		return mav;
		}
		
		//根据ID查询
		@RequestMapping("del")
		public String del(int eid){
			eventService.del(eid);
			return "redirect:getall.do";	
		}
}
